#!/usr/bin/env python
# -*- coding:utf-8 -*- 

from flask_restful import Resource, reqparse
from flask import jsonify

from controller.noticeController import NoticeController
from utils import commons
from utils.response_code import RET, error_map_EN
import json


class NoticeResource(Resource):

    # get
    @classmethod
    def get(cls, notice_id=None):
        if notice_id:
            kwargs = {
                'notice_id': notice_id,
                'is_delete': 0
            }

            res = NoticeController.get(**kwargs)
            if res['code'] == RET.OK:
                if not res['data']:
                    return jsonify(code=RET.OK, message=error_map_EN[RET.OK], data=[])
                
                # 确保返回所有需要的字段
                data = res['data'][0] if isinstance(res['data'], list) else res['data']
                filtered_data = {
                    'notice_id': data.get('notice_id'),
                    'publisher_id': data.get('publisher_id'),
                    'title': data.get('title'),
                    'content': data.get('content'),
                    'notice_file_url': data.get('notice_file_url'),
                    'status': data.get('status'),
                    'publish_time': data.get('publish_time'),
                    'is_recommend': data.get('is_recommend'),
                    'is_delete': data.get('is_delete')
                }
                return jsonify(code=res['code'], message=res['message'], data=filtered_data)
            else:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])

        parser = reqparse.RequestParser()
        parser.add_argument('auto_id', location='args', required=False, help='auto_id参数类型不正确或缺失')
        parser.add_argument('notice_id', location='args', required=False, help='notice_id参数类型不正确或缺失')
        parser.add_argument('publisher_id', location='args', required=False, help='publisher_id参数类型不正确或缺失')
        parser.add_argument('title', location='args', required=False, help='title参数类型不正确或缺失')
        parser.add_argument('content', location='args', required=False, help='content参数类型不正确或缺失')
        parser.add_argument('notice_file_url', location='args', required=False, help='notice_file_url参数类型不正确或缺失')
        parser.add_argument('status', location='args', required=False, help='status参数类型不正确或缺失')
        parser.add_argument('publish_time', location='args', required=False, help='publish_time参数类型不正确或缺失')
        parser.add_argument('is_recommend', location='args', required=False, help='is_recommend参数类型不正确或缺失')
        parser.add_argument('is_delete', location='args', required=False, help='is_delete参数类型不正确或缺失')
        parser.add_argument('create_time', location='args', required=False, help='create_time参数类型不正确或缺失')
        parser.add_argument('update_time', location='args', required=False, help='update_time参数类型不正确或缺失')
        
        parser.add_argument('Page', location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', location='args', required=False, help='Size参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = NoticeController.get(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalPage=res['totalPage'], totalCount=res['totalCount'])
        else:
            return jsonify(code=res['code'], message=res['message'], data=res['data']) 

    
    # delete
    @classmethod
    def delete(cls, notice_id=None):
        if notice_id:
            kwargs = {
                'notice_id': notice_id
            }

        else:
            return jsonify(code=RET.PARAMERR, message=error_map_EN[RET.PARAMERR], data='id不能为空')

        res = NoticeController.delete(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    
    # put
    @classmethod
    def put(cls, notice_id):
        if not notice_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('publisher_id', location='json', required=False, help='publisher_id参数类型不正确或缺失')
        parser.add_argument('title', location='json', required=False, help='title参数类型不正确或缺失')
        parser.add_argument('content', location='json', required=False, help='content参数类型不正确或缺失')
        parser.add_argument('notice_file_url', location='json', required=False, help='notice_file_url参数类型不正确或缺失')
        parser.add_argument('publish_time', location='json', required=False, help='publish_time参数类型不正确或缺失')
        parser.add_argument('create_time', location='json', required=False, help='create_time参数类型不正确或缺失')
        parser.add_argument('update_time', location='json', required=False, help='update_time参数类型不正确或缺失')
        parser.add_argument('status', location='json', required=False, help='status参数类型不正确或缺失')
        parser.add_argument('is_recommend', location='json', required=False, help='is_recommend参数类型不正确或缺失')
        parser.add_argument('is_delete', location='json', required=False, help='is_delete参数类型不正确或缺失')
        
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['notice_id'] = notice_id

        res = NoticeController.update(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    
    # add
    @classmethod
    def post(cls):
        '''
        NoticeList: Pass in values in JSON jsonat to batch add
        eg.[{k1:v1,k2:v2,...},...]
        '''
        parser = reqparse.RequestParser()
        parser.add_argument('NoticeList', type=str, location='json', required=False, help='NoticeList参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        if kwargs.get('NoticeList'):
            kwargs['NoticeList'] = json.loads(kwargs['NoticeList'])
            for data in kwargs['NoticeList']:
                for key in ['status', 'is_recommend', 'is_delete']:
                    data.pop(key, None)
            res = NoticeController.add_list(**kwargs)

        else:
            parser.add_argument('publisher_id', location='json', required=True, help='publisher_id参数类型不正确或缺失')
            parser.add_argument('title', location='json', required=False, help='title参数类型不正确或缺失')
            parser.add_argument('content', location='json', required=True, help='content参数类型不正确或缺失')
            parser.add_argument('notice_file_url', location='json', required=False, help='notice_file_url参数类型不正确或缺失')
            parser.add_argument('publish_time', location='json', required=False, help='publish_time参数类型不正确或缺失')
            parser.add_argument('create_time', location='json', required=False, help='create_time参数类型不正确或缺失')
            parser.add_argument('update_time', location='json', required=False, help='update_time参数类型不正确或缺失')
            
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)

            res = NoticeController.add(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])
